package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.elements.WeighDomain;
import ch.fhnw.qualiscan.application.pages.NewSurvey;
import ch.fhnw.qualiscan.core.db.helpers.questionWeightArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Question;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class NewSurveyTab2 extends VerticalLayout {
	private NewSurvey newSurvey;
	private List<WeighDomain> weighList;

	public NewSurveyTab2(NewSurvey newSurvey) {
		setMargin(true);
		setSpacing(true);
		
		this.newSurvey = newSurvey;
	}

	public void buildWeighing() {
		newSurvey.selectedDomains = new ArrayList<Domain>();
		List<Domain> domainList = new ArrayList<Domain>();
		Iterator<Domain> domainIterator = newSurvey.survey.getDomains().iterator();
		
		while(domainIterator.hasNext()) {
			domainList.add(domainIterator.next());
		}
		
		int size = domainList.size();
		for (int a = 0; a < size; a++) {
			Domain nextDomain = domainList.get(0);
			
			for(int o = 0; o < domainList.size(); o++) {
				if(nextDomain.getId() > domainList.get(o).getId()) {
					nextDomain = domainList.get(o);
				}
			}
			
			newSurvey.selectedDomains.add(nextDomain);
			domainList.remove(nextDomain);
		}

		addComponent(newSurvey.textHelper.p(TM.get("newsurvey.tab2.desc")));
		
		addComponent(addWeighHeader());
		
		weighList = new ArrayList<WeighDomain>();
		

		for(int i = 0; i < newSurvey.selectedDomains.size(); i++) {
			addDomainItem(newSurvey.selectedDomains.get(i));
		}
		
		// next step button
		HorizontalLayout buttonWrap = new HorizontalLayout();
		buttonWrap.setMargin(true, false, false, false);
		
		Button saveButton = new Button(TM.get("action.next"), new ClickListener() {
			public void buttonClick(ClickEvent event) {
				newSurvey.questionWeights = new ArrayList<questionWeightArgs>();

				for(int o = 0; o < weighList.size(); o++) {
					WeighDomain weighDomain = weighList.get(o);
					int weigh = weighDomain.getWeight();
					Domain persistentDomain = newSurvey.domainModel.getDomainById(weighDomain.domain.getId());
					Iterator<Question> questionIterator = persistentDomain.getQuestions().iterator();
					
					while(questionIterator.hasNext()) {
						
						questionWeightArgs qwa = new questionWeightArgs();
						qwa.setQuestion(questionIterator.next());
						qwa.setWeight(weigh);
						
						newSurvey.questionWeights.add(qwa);
					}
				}
				
				newSurvey.tab3.setEnabled(true);
				newSurvey.tabSheet.setSelectedTab(newSurvey.TabComponent3);
				newSurvey.tab2.setEnabled(false);
			}
		});
		saveButton.setIcon(Config.ICON_GO_TAB);
		buttonWrap.addComponent(saveButton);
		
		this.addComponent(buttonWrap);
	}

	
	public void addDomainItem(Domain d) {
		// load WeighDomain item
		WeighDomain weighDomain = new WeighDomain(d);
		
		//Save to List
		weighList.add(weighDomain);
		
		// add component to layout
		addComponent(weighDomain);
	}
	
	public HorizontalLayout addWeighHeader() {
		// header
		HorizontalLayout header = new HorizontalLayout();
		header.setWidth("800px");
		header.setMargin(true, false, true, false);
		
		HorizontalLayout headerForm = new HorizontalLayout();
		
		// first column (agree)
		Label c1 = new Label(TM.get("weight.1"));
		c1.setStyleName("weight1");
		c1.setHeight("100%");
		
		// second column 
		Label c2 = new Label(TM.get("weight.2"));
		c2.setStyleName("weight2");
		c2.setHeight("100%");
		
		// second column 
		Label c3 = new Label(TM.get("weight.3"));
		c3.setStyleName("weight3");
		c3.setHeight("100%");

		headerForm.addComponent(c1);
		headerForm.addComponent(c2);
		headerForm.addComponent(c3);

		headerForm.setWidth("270px");
		headerForm.setHeight("35px");
		headerForm.setStyleName("formColumns");
		
		Label headerSpacer = new Label("");
		header.addComponent(headerSpacer);
		header.addComponent(headerForm);
		header.setExpandRatio(headerSpacer, 1.0f);

		return header;
	}
	
}
